import requests
from bs4 import BeautifulSoup
import csv

all_info = []
for num in range(1,40):
    url = 'https://hf.lianjia.com/zufang/pg' + str(num)
    try:
        html = requests.get(url,headers={'user-agent':'chrome'})
        #print(html.text)
    except:
        print('获取网页失败')
        
    soup = BeautifulSoup(html.text,'html.parser')
    print('正在抓取第%d页' % num)       
    content_list = soup.find_all(class_ = 'content__list--item--main')

    for content in content_list:
        rent_info = []
        link = content.p.a['href']
        #print(link)
        title = (content.p.a).text.strip()
        #print(title)
        rent_info.append(title)
        rent_info.append(link)
        place = (content.find(class_ = 'content__list--item--des')).get_text(strip=True).split('/')
        #print(place)
        for i in place:
            rent_info.append(''.join(i.split()))
        decoration = content.find(class_ = 'content__list--item--bottom oneline').get_text(strip=True)
        #print(decoration)
        rent_info.append(decoration)
        price = content.find(class_ = 'content__list--item-price').get_text(strip=True)
        #print(price)
        rent_info.append(price)
        #print(rent_info)
        all_info.append(rent_info)
#print(all_info)


try:
    with open('爬虫\\租房信息2.csv','w',encoding='gbk',newline='') as f:
        f_csv = csv.writer(f)
        for i in all_info:
            f_csv.writerow(i)
except:
    print('写入失败')